﻿using System;
using System.Linq;
using System.Windows;
using UIConfigTool.Models.CDS;
using UIConfigTool.Models.Loadport;
using UIConfigTool.Models.PM;
using UIConfigTool.ViewModel;

namespace UIConfigTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ConfigViewModel ViewModel { get; set; }
        public object SelectedObject { get; set; }  // 绑定给 PropertyGrid 的 SelectedObject
        public MainWindow()
        {
            InitializeComponent();

            ViewModel = new ConfigViewModel();


            // 设置窗口的数据上下文
            DataContext = ViewModel;
        }
        private void MyTreeView_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
        {

            var selectedItem = MyTreeView.SelectedItem;
            if (selectedItem != null)
            {
                if (selectedItem is CDSDevice selectedDevice)
                {
                    MyPropertyGrid.SelectedObject = selectedDevice;// 更新 PropertyGrid 显示
                }
                else if (selectedItem is PMDevice pm)
                {
                    MyPropertyGrid.SelectedObject = pm;// 更新 PropertyGrid 显示
                }
                else if (selectedItem is LoadportDevice lp)
                {
                    MyPropertyGrid.SelectedObject = lp;// 更新 PropertyGrid 显示
                }
                else if (selectedItem is Heater selectedHeater)
                {
                    MyPropertyGrid.SelectedObject = selectedHeater;// 更新 PropertyGrid 显示
                }
                else if (selectedItem is InletPipe selectedPipe)
                {
                    MyPropertyGrid.SelectedObject = selectedPipe;// 更新 PropertyGrid 显示
                }
                else if (selectedItem is FlowControl selectedFlowControl)
                {
                    MyPropertyGrid.SelectedObject = selectedFlowControl;// 更新 PropertyGrid 显示
                }
                else if (selectedItem is FlowMeter flowMeter)
                {
                    MyPropertyGrid.SelectedObject = flowMeter;// 更新 PropertyGrid 显示
                }
            }
            else
            {
                MessageBox.Show("请先选择一个节点！");
            }

        }

        // 右键菜单 "Refresh" 事件处理
        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            // 刷新树形结构（可以根据实际需求进行刷新操作）
            MessageBox.Show("树形结构已刷新！");
        }
    }
}
